/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import radiostation.AppControl;
import radiostation_POJO.Playlist;

/**
 *
 * @author eliastsourapas
 */
public class R16_Playlist_View extends javax.swing.JFrame {
    public boolean createMode; //δημιουγία λίστας τραγουδιών
    public boolean editMode; //επεξεργασία λίστας τραγουδιών
    public boolean deleteMode; //διαγραφή λίστας τραγουδιών
    private List<Playlist> playlists = new ArrayList();
    private EntityManager em;
    public Playlist newPls; //Η λίστα τραγουδιών που τροποποιείται
    AppControl ap; //Αντικείμενο για τον χειρισμό της ΒΔ

    /**
     * Creates new form R16_Playlist_View
     */
    public R16_Playlist_View() {
        initComponents();
    }

    public boolean getCreateMode() {
        return createMode;
    }
    
    public void setCreateMode(boolean createMode) {
        this.createMode = createMode;
    }
    
    public boolean getEditMode() {
        return editMode;
    }
    
    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }
    
    public boolean getDeleteMode() {
        return deleteMode;
    }
    
    public void setDeleteMode(boolean deleteMode) {
        this.deleteMode = deleteMode;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationPUEntityManager0 = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationPU").createEntityManager();
        playlistQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager0.createQuery("SELECT p FROM Playlist p");
        playlistList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : playlistQuery.getResultList();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablePlaylists = new javax.swing.JTable();
        newPlaylist = new javax.swing.JButton();
        alterPlaylist = new javax.swing.JButton();
        deletePlaylist = new javax.swing.JButton();
        importPlaylist = new javax.swing.JButton();
        exportPlaylist = new javax.swing.JButton();
        exitPlaylist = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        jLabel1.setText("Λίστες Τραγουδιών");

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, playlistList, tablePlaylists);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${plName}"));
        columnBinding.setColumnName("Όνομα Λίστας");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${plDescription}"));
        columnBinding.setColumnName("Περιγραφή");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${plCreationDate}"));
        columnBinding.setColumnName("Ημ/νία Δημιουργίας");
        columnBinding.setColumnClass(java.util.Date.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(tablePlaylists);

        newPlaylist.setText("Δημιουργία");
        newPlaylist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newPlaylistActionPerformed(evt);
            }
        });

        alterPlaylist.setText("Επεξεργασία");
        alterPlaylist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterPlaylistActionPerformed(evt);
            }
        });

        deletePlaylist.setText("Διαγραφή");
        deletePlaylist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deletePlaylistActionPerformed(evt);
            }
        });

        importPlaylist.setText("Εισαγωγή...");

        exportPlaylist.setText("Εξαγωγή...");

        exitPlaylist.setText("Έξοδος");
        exitPlaylist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitPlaylistActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(217, 217, 217)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(newPlaylist, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(alterPlaylist, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(deletePlaylist, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(importPlaylist, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(exportPlaylist, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(exitPlaylist, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newPlaylist)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(alterPlaylist)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deletePlaylist)
                        .addGap(61, 61, 61)
                        .addComponent(importPlaylist)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(exportPlaylist)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(exitPlaylist)))
                .addGap(28, 28, 28))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void newPlaylistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newPlaylistActionPerformed
        // TODO add your handling code here:
        this.createMode = true;
        this.editMode = false;
        this.deleteMode = false;
        this.setVisible(true);
        new R17_Playlist_Management(this).setVisible(true);
    }//GEN-LAST:event_newPlaylistActionPerformed

    private void alterPlaylistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterPlaylistActionPerformed
        // TODO add your handling code here:
        this.createMode = false;
        this.editMode = true;
        this.deleteMode = false;
        if(tablePlaylists.getSelectedRow()==-1)
            JOptionPane.showMessageDialog(null, "Δεν έχει επιλεγεί λίστα!");
        else{
            for(Playlist pls: playlists){
                if(pls.getPlaylistId()==tablePlaylists.getValueAt(tablePlaylists.getSelectedRow(), 0)){
                    newPls = pls;
                    new R17_Playlist_Management(this).setVisible(true);
                }
            }
        }
    }//GEN-LAST:event_alterPlaylistActionPerformed

    private void exitPlaylistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitPlaylistActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_exitPlaylistActionPerformed

    private void deletePlaylistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deletePlaylistActionPerformed
        // TODO add your handling code here:
        this.createMode = false;
        this.editMode = false;
        this.deleteMode = true;
        if(tablePlaylists.getSelectedRow()==-1)
            JOptionPane.showMessageDialog(null, "Δεν έχει επιλεγεί λίστα!");
        else{
            for(Playlist pls: playlists){
                if(pls.getPlaylistId()==tablePlaylists.getValueAt(tablePlaylists.getSelectedRow(), 0)){
                    newPls = pls;
                    new R17_Playlist_Management(this).setVisible(true);
                }
            }
        }
    }//GEN-LAST:event_deletePlaylistActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(R16_Playlist_View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(R16_Playlist_View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(R16_Playlist_View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(R16_Playlist_View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new R16_Playlist_View().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager RadioStationPUEntityManager0;
    private javax.swing.JButton alterPlaylist;
    private javax.swing.JButton deletePlaylist;
    private javax.swing.JButton exitPlaylist;
    private javax.swing.JButton exportPlaylist;
    private javax.swing.JButton importPlaylist;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton newPlaylist;
    private java.util.List<radiostation_POJO.Playlist> playlistList;
    private javax.persistence.Query playlistQuery;
    private javax.swing.JTable tablePlaylists;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
